package com.greendragon.persistence.dao.impl; 

import java.sql.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.greendragon.persistence.dao.IArticalDao;
import com.greendragon.persistence.domain.Artical;
import com.greendragon.persistence.enmu.FetchOrderType;
import com.greendragon.persistence.enmu.LockType;


/** 
 * @author wulibin, exceptsky@gmail.com, 2011-9-8 下午11:24:28
 * 
 */
public class ArticalDaoImpl extends BaseDao<Artical> implements IArticalDao{
	private static Logger          logger           = Logger.getLogger(ArticalDaoImpl.class);
	
	@Override
	public List<Artical> getByDate(Date startDate, Date endDate, int offset, int length, LockType lockType, FetchOrderType orderType) {
		String hql = "from Artical as a where a.createdAt between ? and ?"+getLockSql("a", lockType)+getOrderSql("a", orderType);
		return selectByPage(hql, new Date[]{startDate, endDate}, offset, length);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public long getCountByDate(Date startDate, Date endDate, LockType lockType) {
		String hql = "select count(a.id) from Artical as a where a.createdAt between ? and ?"+getLockSql("a", lockType);
		try {
			List rs = getHibernateTemplate().find(hql, new Date[]{startDate, endDate});
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}
}
 